package com.mkb;

import com.mkb.common.ModuleURL;
import com.mkb.conf.AdminLoginAuthInterceptor;
import com.mkb.conf.BusinessExceptionResolver;
import com.mkb.utils.WinowUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.jmx.support.RegistrationPolicy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.MultipartConfigElement;
import java.util.List;
import java.util.Properties;

@SpringBootApplication
@EnableAspectJAutoProxy(exposeProxy=true)
@EnableScheduling
@EnableAsync
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class Application extends WebMvcConfigurerAdapter {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler(ModuleURL.BASE_PATH + "/static/**")
				.addResourceLocations("classpath:/static/");


		if (WinowUtil.isWindows()) {
			registry.addResourceHandler("/external/**").addResourceLocations("file:///D:/map/wlmq/");
		} else {
			registry.addResourceHandler("/external/**").addResourceLocations("file:/apply/wlmq/wlmq/");
		}

	}

	@Bean
	public AdminLoginAuthInterceptor loginInterceptor() {
		return new AdminLoginAuthInterceptor();
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(loginInterceptor()).addPathPatterns("/**")
				.excludePathPatterns("/test/**")
				.excludePathPatterns("/login")
				.excludePathPatterns("/page/login")
				.excludePathPatterns("/js/**")
				.excludePathPatterns("/css/**")
				.excludePathPatterns("/img/**")
				.excludePathPatterns("/patterns/**")
				.excludePathPatterns("/static/**")
				;
		super.addInterceptors(registry);
	}

	@Override
	public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
		BusinessExceptionResolver exceptionResolver = new BusinessExceptionResolver();
		Properties mappings = new Properties();
		mappings.put("java.lang.Exception", "inc/500");

		exceptionResolver.setExceptionMappings(mappings);
		exceptionResolvers.add(exceptionResolver);
		super.configureHandlerExceptionResolvers(exceptionResolvers);
	}

	@Bean
	public MultipartConfigElement multipartConfigElement() {
		MultipartConfigFactory factory = new MultipartConfigFactory();
		factory.setMaxFileSize("100MB");
		factory.setMaxRequestSize("30000MB");
		return factory.createMultipartConfig();
	}



	@Bean
	RestTemplate restTemplate() {
		return new RestTemplate();
	}


}
